package com.snailhd.crawler;

import com.alibaba.fastjson.JSON;
import com.snailhd.common.util.HttpClientUtil;
import com.snailhd.constant.SinaEnum;
import com.snailhd.entity.Stock;
import com.snailhd.mapper.StockDayMapper;
import com.snailhd.mapper.StockMapper;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 爬取最新股票列表
 * @author hd
 * @date 2019-10-07 15:29
 */
@Slf4j
@Component
public class CrawlStockListExector {

    @Resource
    StockMapper stockMapper;

    private final static String CRAWL_URL = "http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData";

    private final static String END_SIGN = "null";

    public List<Stock> exec(SinaEnum ... sinaEnums) throws Exception {
        List<Stock> stocks = new ArrayList<>();
        for(SinaEnum sinaEnum :sinaEnums ){
            int page = 1;
            while (true){
                CrawlParams cp = new CrawlParams(sinaEnum,page);
                String resultStr = HttpClientUtil.get("爬取股票列表",CRAWL_URL,BeanUtils.describe(cp));
                Thread.sleep(800);
                if(END_SIGN.equals(resultStr)) {break;}
                List<Stock> list = JSON.parseArray(resultStr, Stock.class);
                stocks.addAll(list);
                page++;
            }
        }
        return stocks;
    }



    @Setter
    @Getter
    public class CrawlParams{
        private Integer page=1;
        private Integer num=100;
        private String sort="symbol";
        private String asc="1";
        //分类 sh_a
        private String node;
        private String symbol;
        private String _s_r_a="page";

        public CrawlParams() {
        }

        public CrawlParams(SinaEnum sinaEnum,int page) {
            this.node = sinaEnum.name();
            this.page = page;
        }

    }


}
